<template>
    <div>
        <div class="table-title">
            库存添加
        </div>
        <div class="table-padding">
            <div style="margin: 20px;"></div>
            <el-form :model="modelForm" ref="modelForm" label-width="100px" class="demo-ruleForm" style="width:500px">
                <el-form-item label=" " prop="materialId" :rules="[]">
                <el-input placeholder="请输入物品id" v-model="modelForm.materialId">
                    <template slot="prepend">
                        <el-tooltip placement="top" content="请填写物品id">
                            <el-button tabIndex="-1">物品id</el-button>
                        </el-tooltip>
                    </template>
                </el-input>
                </el-form-item>
                <el-form-item label=" " prop="warehouseId" :rules="[]">
                <el-input placeholder="请输入仓库id" v-model="modelForm.warehouseId">
                    <template slot="prepend">
                        <el-tooltip placement="top" content="请填写仓库id">
                            <el-button tabIndex="-1">仓库id</el-button>
                        </el-tooltip>
                    </template>
                </el-input>
                </el-form-item>
                <el-form-item label=" " prop="count" :rules="[]">
                <el-input placeholder="请输入库存数量" v-model.number="modelForm.count">
                  <template slot="prepend">
                      <el-tooltip placement="top" content="请填写库存数量">
                          <el-button tabIndex="-1">库存数量</el-button>
                      </el-tooltip>
                  </template>
                </el-input>
                </el-form-item>
                <el-form-item label=" " prop="cost" :rules="[]">
                <el-input placeholder="请输入成本均价" v-model.number="modelForm.cost">
                  <template slot="prepend">
                      <el-tooltip placement="top" content="请填写成本均价">
                          <el-button tabIndex="-1">成本均价</el-button>
                      </el-tooltip>
                  </template>
                </el-input>
                </el-form-item>
                <el-form-item label=" " prop="amount" :rules="[]">
                <el-input placeholder="请输入库存金额" v-model.number="modelForm.amount">
                  <template slot="prepend">
                      <el-tooltip placement="top" content="请填写库存金额">
                          <el-button tabIndex="-1">库存金额</el-button>
                      </el-tooltip>
                  </template>
                </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('modelForm')">提交</el-button>
                     <el-button  type="success" @click="goBack()">返回</el-button>
                </el-form-item>
          </el-form>
        </div>
    </div>
</template>
<script>
    //注意路径
    export default{
        data(){
            return {
                 modelForm: {
                 }
            };
        },
        created(){
            window.this = this;
            this.initData();
        },
        methods: {
        //页面第一次进来获取表单需要的数据
            initData(){
                this.$axios.get("store/stocks/create").then((data)=>{
                    if(data.body.status==200){
                        this.modelForm=data.body.content.stock            ;
                    }else{
                        this.$message.error(data.body.message);
                    }
                })
            },
            goBack() {
                this.router.go(-1);
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("store/stocks/create",this.modelForm).then((data)=>{
                            if(data.body.status==200){
                                this.$message.success(data.body.message);
                                this.router.go(-1);
                            }else{
                                this.$message.error(data.body.message);
                            }
                        });
                    } else {
                        this.$message.error("请检查输入项");
                        return false;
                    }
                });
            },
            isNum(item,key){
                var r = /^\+?[1-9][0-9]*$/;
                if( ! r.test(item[key])){
                    item[key]="0.00"
                }else{
                    item[key]=item[key]
                }
            },
            isDouble(item,key){
                var isNum=/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
                if( ! isNum.test(item[key])){
                    item[key]="0.00"
                }else{
                    item[key]=item[key]
                }
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },

        }
    }
</script>
<style scoped>

</style>